#!/bin/bash



json_template='
{
  "target_table":"ods.user_action_log",
  "target_date":"2024-04-20",
  "rules":[
      {
        "rule_name":"表行数检查",
        "check_result":line_cnt_@@
      },
      {
        "rule_name":"字段空值个数检查",
        "target_field":"guid",
        "check_result":guid_null_@@
      },
      {
        "rule_name":"字段空值占比检查",
        "target_field":"province",
        "check_result":province_null_@@
      },
      {
        "rule_name":"字段空值占比检查",
        "target_field":"city",
        "check_result":city_null_@@
      },
      {
        "rule_name":"字段空值占比检查",
        "target_field":"region",
        "check_result":region_null_@@
      },
      {
        "rule_name":"字段空值占比检查",
        "target_field":"user_id",
        "check_result":user_id_null_@@
      },
      {
        "rule_name":"字段MAX检查",
        "target_field":"action_time",
        "check_result":action_time_max_@@
      },
      {
        "rule_name":"字段MIN检查",
        "target_field":"action_time",
        "check_result":action_time_min_@@
      }
  ]
}
'



cur_day=$(date +%Y-%m-%d)
pre_day=$(date -d'-1 day' +%Y-%m-%d)

sql="
select
  count(1) as cnt,
  sum(case when guid is null then 1 else 0 end) as guid_null_cnt,
  sum(case when province is null then 1 else 0 end)/count(1) as province_null_cnt,
  sum(case when city is null then 1 else 0 end)/count(1) as city_null_cnt,
  sum(case when region is null then 1 else 0 end)/count(1) as region_null_cnt,
  sum(case when user_id is null then 1 else 0 end) as user_id_null_cnt,
  max(action_time) as max_action_time,
  min(action_time) as min_action_time
from dwd.user_action_detail
where  dt='${pre_day}';
"

res=$(beeline -u jdbc:hive2://doitedu:10000 -n root --outputformat=csv2 --showHeader=false -e "\"$sql\"")

# 切割上面的结果，取出各个规则的统计结果


# 然后对前面的json模板，用这些结果，使用sed 命令替换掉里面的占位符


# 然后把检查结果json，通过接口请求，发送给数据平台的质量管理系统

